/* Paginate Table Script Â©2008 John Davenport Scheuer
   as first seen in http://www.dynamicdrive.com/forums/
   username: jscheuer1 - This Notice Must Remain for Legal Use
   */

// you can init as many tables as you like in here by id: paginateTable('id', 0, num_max_rows);

////////////////////// Stop Editing //////////////////////

function paginateTable(table, way){
if(way!=0){
window.scrollTo(0,0);
}
entries_per_page=document.getElementById("id_entries_per_page");
max=entries_per_page.value;
max? paginateTable.max[table] = max : max = paginateTable.max[table];
var r = document.getElementById(table).rows, i = 0;

var page_number=parseInt(r.length/(document.getElementById("id_entries_per_page").value));
page_number+=r.length%(document.getElementById("id_entries_per_page").value)>0? 1:0;
document.getElementById("label_pages").innerHTML=page_number;

var current_page=parseInt(document.getElementById("current_page").innerText);
if (way==0 && r.length>0){

        document.getElementById("current_page").innerHTML='1';
}

else if ((way!=-1 || (current_page!='1')) && (way!=1 || current_page!=page_number)){
        current_page+=way;
        document.getElementById("current_page").innerHTML=current_page;
}

if(way==0 && r.length==0){
	document.getElementById("current_page").innerHTML='0';
}


way = way == 1? 1 : way == -1? 0 : -1;

for(i; i < r.length; ++i) // find current start point
 if(r[i].style.display != 'none')
  break;
for(i; i < r.length; ++i){ // continue on to find current end point
 if(r[i].style.display == 'none'){
  paginateTable.endPoint[table] = i;
  break;
 };
 paginateTable.endPoint[table] = 0; // if no end point found, table is 'virgin' or at end
};
if(way == 1 && r[r.length - 1].style.display != 'none') return; // table was already at the end and we tried to move forward
// if moving forward, start will be old end, else start will be old start - max or 0, whichever is greater:
paginateTable.startPoint[table] = way? paginateTable.endPoint[table] : Math.max( 0, paginateTable.startPoint[table] - max);
paginateTable.endPoint[table] = paginateTable.startPoint[table] + --max; // new end will be new start + max - 1
for (i = r.length - 1; i > -1; --i) // set display of rows based upon whether or not they are in range of the calculated start/end points
 r[i].style.display = i < paginateTable.startPoint[table] || i > paginateTable.endPoint[table]? 'none' : '';
};

paginateTable.startPoint = {};
paginateTable.endPoint = {};
paginateTable.max = {};

if(window.addEventListener)
window.addEventListener('load', paginateTable.init, false);
else if (window.attachEvent)
window.attachEvent('onload', paginateTable.init);


